<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isAll">
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model.trim="task"
      @keyup.enter='add'
    />
  </header>
</template>

<script>
import {nanoid} from 'nanoid'
import {mapGetters} from 'vuex'

export default {
  data(){
    return {
      task: '',
    }
  },
  methods:{
    add(){
      // 1.非空判断
      if(!this.task) return alert('请输入任务')
      // 2.包装对象 添加任务id
       const todoObj = {id:nanoid(), task:this.task, done:false}
      // 3.添加
      this.$store.commit('list/ADDTODO', todoObj)
      // 4.置空输入框
      this.task = ''
    }
  },
  computed:{
    ...mapGetters(['showlist']),
    isAll:{
       get(){
        return  this.showlist.length && this.showlist.every(item => item.done)
       },
       set(val){
        this.$store.commit('list/IS_ALL', val)
       }
    }
  }
 


 
}
</script>
